package com.sichard.waterfullview.request.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.sichard.waterfullview.R;
import com.sichard.waterfullview.request.data.WaterfallEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * <br>类表述：
 * <br>详细描述：
 * <br><b>Author Caoshichao </b></br>
 * <br><b>Date  2020/4/15</b></br>
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.WaterfallViewHolder> {
    private Context mContext;
    private List<WaterfallEntity.HitsEntity> mImageList;

    public RecyclerViewAdapter(Context context) {
        mContext = context;
        mImageList = new ArrayList<>();
    }

    @NonNull
    @Override
    public WaterfallViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new WaterfallViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_recycler_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull WaterfallViewHolder holder, int position) {
        final WaterfallEntity.HitsEntity hitsEntity = mImageList.get(position);
        LinearLayout.LayoutParams params = getLayoutParams(holder, hitsEntity);

        Glide.with(mContext).load(hitsEntity.previewURL).override(params.width, params.height)
                .placeholder(R.mipmap.loading).into(holder.mImageView);
        holder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse(hitsEntity.pageURL);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                mContext.startActivity(intent);
            }
        });

    }

    @NonNull
    private LinearLayout.LayoutParams getLayoutParams(@NonNull WaterfallViewHolder holder, WaterfallEntity.HitsEntity hitsEntity) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mImageView.getLayoutParams();
        float itemWidth = (getScreenWidth() - 24f) / 2;
        params.width = (int) itemWidth;
        float scale = itemWidth / hitsEntity.previewWidth;
        params.height = (int) (hitsEntity.previewHeight * scale);
        holder.mImageView.setLayoutParams(params);
        return params;
    }

    private int getScreenWidth() {
        return mContext.getResources().getDisplayMetrics().widthPixels;
    }

    @Override
    public int getItemCount() {
        return mImageList.size();
    }

    public void addData(List<WaterfallEntity.HitsEntity> result) {
        mImageList.addAll(result);
        notifyDataSetChanged();
    }

    class WaterfallViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView;

        WaterfallViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageView);
        }
    }
}
